import xml.etree.ElementTree as ET

from nltk.translate.meteor_score import meteor_score


def data_from_xml(file_path):
    tree = ET.parse(file_path)
    root = tree.getroot()
    data_list = []
    for elem in root.iter('string'):
        data_list.append(elem.text)
    return data_list


def meteor_score_fenci(candidate_translations, reference_translations):
    b = []
    for i, candidate_translation in enumerate(candidate_translations):
        print("---"*18)
        # 将字符串分词成列表
        candidate_tokens = candidate_translation.split()
        print(f"candidate_tokens:{candidate_tokens}")
        reference_tokens = []
        for reference in reference_translations[i]:
            reference_tokens.append(reference.split())
            print(f"reference_tokens:{reference_tokens}")
        score = meteor_score(reference_tokens, candidate_tokens)
        print(f"Meteor分数（句子{i + 1}）: {score}")
        b.append(float(score))
    a = 0
    g = int(len(b))
    for f in b:
        a = a + f
        # 求平均得分
    score_average = a/g
    print("---" * 18)
    print(f"meteor_score最终平均得分为{score_average}")
